package com.micro.service.auth.server.config.oauth2;

import com.google.common.collect.Maps;
import com.micro.service.auth.server.model.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.Map;

/**
 * @author lvxiucai
 * @description token增强,自定义返回信息
 * @date 2019/9/6
 */
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

        if (accessToken instanceof DefaultOAuth2AccessToken) {
            DefaultOAuth2AccessToken token = ((DefaultOAuth2AccessToken) accessToken);
            //此处可以设置自己想要的token值
            User user = (User)authentication.getPrincipal();
            Map<String,Object> map = Maps.newHashMap();
            map.put("userId", user.getId());
            token.setAdditionalInformation(map);
            return token;
        }
        return null;
    }
}
